package my.test.groups.common;

import java.util.ArrayList;

/**
 * @Author 许玉康
 * @Date 2019/9/20 16:35
 */
public class client {

    public static void main(String[] args) {
        //首先产品总经理
        IRoot ceo = new Root("王大麻子", "总经理" ,100000);
        //把三个部门经理产生出来
        IBranch developDep = new Branch("赵大瘸子","研发部门经理", 10000);
        IBranch salesDep = new Branch("马耳掴子", "销售部经理", 20000);
        IBranch financeDep = new Branch("赵三驼子", "财务部经理", 30000);

        //再把三个小组长生产出来
        IBranch firstDevGroup = new Branch("杨三也写", "开发组一组组长", 5000);
        IBranch secondDevGroup = new Branch("舞大棒催", "开发组二组组长", 6000);

        //把所有小兵生产出来
        Leaf a  = new Leaf("a", "开发人员", 2000);
        Leaf b  = new Leaf("b", "开发人员", 2000);
        Leaf c  = new Leaf("c", "开发人员", 2000);
        Leaf d  = new Leaf("d", "开发人员", 2000);
        Leaf e  = new Leaf("e", "开发人员", 2000);
        Leaf f  = new Leaf("f", "开发人员", 2000);
        Leaf g  = new Leaf("g", "开发人员", 2000);
        Leaf h  = new Leaf("h", "开发人员", 2000);
        Leaf i  = new Leaf("i", "销售人员", 2000);
        Leaf j  = new Leaf("j", "销售人员", 2000);
        Leaf k  = new Leaf("k", "CEO秘书", 2000);
        Leaf zhengLaoLiu  = new Leaf("郑老六", "研发部副经理", 20000);

        //该产生的人都产出来了，然后我们怎么组装这棵树
        //首先是定义总经理下有三个部门经理
        ceo.add(developDep);
        ceo.add(salesDep);
        ceo.add(financeDep);

        //总经理下还有一个秘书
        ceo.add(k);
        //定义研发部门下的结构
        developDep.add(firstDevGroup);
        developDep.add(zhengLaoLiu);
        //研发部经理下还有一个副总
        developDep.add(zhengLaoLiu);

        //再看销售部下的人员情况
        salesDep.add(h);
        salesDep.add(i);
        //最后一个财务
        financeDep.add(j);

        //打印写完的树状结构
        System.out.println(ceo.getInfo());
        getAllSubordinateInfo(ceo.getSubordinateInfo());
    }


    //遍历所有的树枝节点，打印出信息
    private static void getAllSubordinateInfo(ArrayList subordinateList) {
        int length = subordinateList.size();
        //定义一个ArrayList长度，不要在for循环中每次计算
        for (int i = 0; i < length; i++) {
            Object s = subordinateList.get(i);
            if(s instanceof Leaf) {
                ILeaf employee = (ILeaf) s;
                System.out.println(((Leaf) s).getInfo());
            } else {
                IBranch branch = (IBranch) s;
                System.out.println(branch.getInfo());
                //再递归调用
                getAllSubordinateInfo(branch.getSubordinateInfo());
            }
        }
    }
}
